package com.csdj.s3project.controller;


import com.csdj.s3project.common.CacheConstant;
import com.csdj.s3project.common.WebResult;
import com.csdj.s3project.common.WebResultMsgCode;
import com.csdj.s3project.service.RedisService;
import com.csdj.s3project.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 发送邮件控制器
 */
@RestController
@RequestMapping("/mail")
public class MailController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private JavaMailSender mailSender;
    @Resource
    private RedisService redisService;

    @GetMapping("/send")
    public WebResult sendMail(@RequestParam(value = "email", required = false) String email)
            throws MessagingException {
        log.info("email=" + email);
        String checkCode = RandomUtil.getCheckCode();
        log.info("验证码是:" + checkCode);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage, true);
        //发送方
        mmh.setFrom("3539813629@qq.com");
        //接收方
        mmh.setTo(email.trim());//去除两端空格
        mmh.setSubject("欢迎注册拼图app");
        mmh.setText("<!DOCTYPE html>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<p>校验码:</p>\n" +
                "\t\t<p>" + checkCode + "</p>\n" +
                "\t\t<p>验证码的有效期为15分钟</p>\n" +
                "\t\t<p>如非本人操作, 请忽略</p>\n" +
                "\t</body>\n" +
                "</html>", true);
        try {
            mailSender.send(mimeMessage);
            log.info("邮件已发送。");
            redisService.setString(CacheConstant.TOKEN_PREFIX+email,checkCode,60*60);
            log.info("存入redis的验证码为:"+redisService.getString(CacheConstant.TOKEN_PREFIX+email));
            return new WebResult(WebResultMsgCode.SUCCESS);
        } catch (Exception e) {
            log.error("发送邮件时发生异常了！", e);
            return new WebResult(WebResultMsgCode.FAIL);
        }
    }


    @RequestMapping("/send2")
    public void sendMail2() throws MessagingException {
        String randomCheckCode = RandomUtil.getCheckCode();
        log.info("randomCheckCode=" + randomCheckCode);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        SimpleMailMessage message = new SimpleMailMessage();
        //发送方的qq邮箱
        message.setFrom("2691050847@qq.com");
        //接收方邮箱
        message.setTo("501379782@qq.com");
        //设置标题
        message.setSubject("欢迎注册拼图app");

        //message
        message.setText("校验码:" + randomCheckCode + "" +
                "验证码的有效期为一小时" +
                "如非本人操作, 请忽略");
        try {
            mailSender.send(message);
            log.info("邮件已发送。");
        } catch (Exception e) {
            log.error("发送邮件时发生异常了！", e);
        }
    }


    public static void main(String[] args) {
        System.out.println("验证码:" + RandomUtil.getCheckCode());

    }

}
